﻿#include "UpperDevice.h"

UpperDevice::UpperDevice(QObject *parent) :
    BaseInterface<UpperDevice>(parent)
{
    qDebug()<<"UpperDevice runing-1";
    m_pInstance = this;
    cloudProtocol = new CloudProtocol(this);
    this->registerProtocol(cloudProtocol);

    swKNXProtocol = new SWKNXProtocol(this);
    this->registerProtocol(swKNXProtocol);

    registerCmdHandler(UpperSearchCmd::FunctionCode,UpperSearchCmd::staticMetaObject, &UpperDevice::upperSearchCmdHandle);
    registerCmdHandler(UpperSetMacParamCmd::FunctionCode,UpperSetMacParamCmd::staticMetaObject, &UpperDevice::upperSetMacParamCmdHandle);
    registerCmdHandler(UpperSetNetParamCmd::FunctionCode,UpperSetNetParamCmd::staticMetaObject, &UpperDevice::upperSetNetParamCmdHandle);
    registerCmdHandler(UpperSetTempNetParamCmd::FunctionCode,UpperSetTempNetParamCmd::staticMetaObject, &UpperDevice::upperSetTempNetParamCmdHandle);
    registerCmdHandler(DownloadFileCmd::FunctionCode,DownloadFileCmd::staticMetaObject, &UpperDevice::downloadFileCmdHandle);
    registerCmdHandler(RebootCmd::FunctionCode,RebootCmd::staticMetaObject, &UpperDevice::rebootCmdHandle);
    registerCmdHandler(UpperSetKNXMacCmd::FunctionCode,UpperSetKNXMacCmd::staticMetaObject, &UpperDevice::upperSetKNXMacCmdHandle);

    registerFuncHandler(DownloadFileResultFunc::FunctionCode, &UpperDevice::downloadFileResultFuncHandle);
     qDebug()<<"UpperDevice runing-2";
}

void UpperDevice::upperSearchCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Zigbee3ListXml *zigbee3listxml=Zigbee3ListXml::getInstance();
    if(zigbee3listxml->ZigbeeVersion().size()==0)
    {
        qDebug()<<"Query the ZigbeeVersion";
        PortStruct target;
        target.setType("UART");
        target.setName("Zigbee");
        target.setPortName("/dev/ttyS1");
        target.setMode("Polling");
        target.setBaudRate("BAUD115200");
        target.setDataBits("DATA_8");
        target.setParity("NONE");
        target.setStopBits("STOP_1");
        target.setFlowControl("OFF");
        QByteArray SendQueryZigbeeVersionCmd=zigbee3listxml->GetZigbeeVersion();
        emit sendDataSignal(target,SendQueryZigbeeVersionCmd);
    }

    UpperSearchResponseCmd *upperSearchResponseCmd = new UpperSearchResponseCmd();

    quint16 cmdId = sharedCmd->cmdId();
    upperSearchResponseCmd->setCmdId(cmdId);

    MacXml *macXml = MacXml::getInstance();
    QString mac = macXml->mac();
    QString registrant = macXml->registrant();
    QByteArray registrationDateBCD = macXml->registrationDateBCD();
    upperSearchResponseCmd->setMac(mac);
    upperSearchResponseCmd->setRegistrant(registrant);
    upperSearchResponseCmd->setRegistrationDateBCD(registrationDateBCD);

    System *system = System::getInstance();
    NetParam netParam = system->netParam();
    quint8 mode = netParam.mode();
    QString ip = netParam.ip();
    QString mask = netParam.mask();
    QString gateway = netParam.gateway();
    upperSearchResponseCmd->setMode(mode);
    upperSearchResponseCmd->setIp(ip);
    upperSearchResponseCmd->setMask(mask);
    upperSearchResponseCmd->setGateway(gateway);
    upperSearchResponseCmd->setKNXMac(system->getKNXMac());
    upperSearchResponseCmd->setKNXStatus(system->getKNXStatus());

    QSharedPointer<BaseCmd> sharedUpperSearchResponseCmd(upperSearchResponseCmd);
    this->sendCmd(source,sharedUpperSearchResponseCmd);
    qDebug()<<"a zhe wo hao le ma ";
}

void UpperDevice::upperSetMacParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    quint16 cmdId = sharedCmd->cmdId();
    UpperSetMacParamCmd *upperSetMacParamCmd = qobject_cast<UpperSetMacParamCmd*>(sharedCmd.data());

    QString mac = upperSetMacParamCmd->mac();
    if(!mac.contains("SH900-APP"))
       return;
    QString registrant = upperSetMacParamCmd->registrant();
    QByteArray registrationDateBCD = upperSetMacParamCmd->registrationDateBCD();

    MacXml *macXml = MacXml::getInstance();
    bool isSucceed = macXml->modify(mac,registrant,registrationDateBCD);
    if(isSucceed)
    {
        ResponseCmd *responseCmd = new ResponseCmd();
        responseCmd->setCmdId(cmdId);
        responseCmd->setResponseCode(CloudProtocol::Succeed);
        QSharedPointer<BaseCmd> sharedResponseCmd(responseCmd);
        this->sendCmd(source,sharedResponseCmd);
    }
}

void UpperDevice::upperSetKNXMacCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{

    //qDebug()<<"~~~~~~UpperDevice::upperSetKNXMacCmdHandle";
         UpperSetKNXMacCmd *upperSetKNXMacCmd = qobject_cast<UpperSetKNXMacCmd*>(sharedCmd.data());


         setKNXMacCmd *_setKNXMacCmd = new setKNXMacCmd();
         _setKNXMacCmd->setKNXMac(upperSetKNXMacCmd->KNXMac());

         QSharedPointer<BaseCmd> sharedSetKNXMacCmd(_setKNXMacCmd);
         PortStruct target;
         foreach (const BasePort *port , m_portList)
         {

              if((port->portParam().type() == "UART" && port->portParam().name() == "SWKNX"))
              {
                  target = port->portParam();

              }
         }
         this->sendCmd(target,sharedSetKNXMacCmd);


         UpperSetKNXMacResponseCmd * setResponse = new UpperSetKNXMacResponseCmd();
         QSharedPointer<BaseCmd> sharedResponseKNXMacCmd(setResponse);
         foreach (const BasePort *port , m_portList)
         {

              if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
              {
                  target = port->portParam();

              }
         }
         this->sendCmd(target,sharedResponseKNXMacCmd);

         System *system = System::getInstance();
        system->setGlobalStatus(1);

}

void UpperDevice::upperSetNetParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    quint16 cmdId = sharedCmd->cmdId();
    UpperSetNetParamCmd *upperSetNetParamCmd = qobject_cast<UpperSetNetParamCmd*>(sharedCmd.data());

    quint8 mode = upperSetNetParamCmd->mode();
    QString ip = upperSetNetParamCmd->ip();
    QString mask = upperSetNetParamCmd->mask();
    QString gateway = upperSetNetParamCmd->gateway();

    NetParam netParam;
    netParam.setMode(mode);
    netParam.setIp(ip);
    netParam.setMask(mask);
    netParam.setGateway(gateway);

    System *system = System::getInstance();
    bool isSucceed = system->setNetParam(netParam);

    if(isSucceed)
    {
        ResponseCmd *responseCmd = new ResponseCmd();
        responseCmd->setCmdId(cmdId);
        responseCmd->setResponseCode(CloudProtocol::Succeed);
        QSharedPointer<BaseCmd> sharedResponseCmd(responseCmd);
        this->sendCmd(source,sharedResponseCmd);
    }   

}

void UpperDevice::upperSetTempNetParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    quint16 cmdId = sharedCmd->cmdId();
    UpperSetTempNetParamCmd *upperSetTempNetParamCmd = qobject_cast<UpperSetTempNetParamCmd*>(sharedCmd.data());

    quint8 mode = upperSetTempNetParamCmd->mode();

    System *system = System::getInstance();
    bool isSucceed = system->setTempNetParam(mode);

    if(isSucceed)
    {
        ResponseCmd *responseCmd = new ResponseCmd();
        responseCmd->setCmdId(cmdId);
        responseCmd->setResponseCode(CloudProtocol::Succeed);
        QSharedPointer<BaseCmd> sharedResponseCmd(responseCmd);
        this->sendCmd(source,sharedResponseCmd);
    }
}

void UpperDevice::downloadFileCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    DownloadFileCmd *downloadFileCmd = qobject_cast<DownloadFileCmd*>(sharedCmd.data());
    quint8 fileType = downloadFileCmd->fileType();
    QString url = downloadFileCmd->url();
    QMap<QString,QString> paramMap = downloadFileCmd->paramMap();

    DownloadFileFunc *downloadFileFunc = new DownloadFileFunc();
    downloadFileFunc->setFileType(fileType);
    downloadFileFunc->setUrl(url);
    downloadFileFunc->setParamMap(paramMap);
    downloadFileFunc->setSource(source);
    QSharedPointer<BaseFunc> sharedDownloadFileFunc(downloadFileFunc);
    emit transferFuncSignal(sharedDownloadFileFunc);
}

void UpperDevice::rebootCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);
    System *system = System::getInstance();
    system->rebootSystem();
}

void UpperDevice::downloadFileResultFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
    DownloadFileResultFunc *downloadFileResultFunc = qobject_cast<DownloadFileResultFunc*>(sharedFunc.data());
    int result = downloadFileResultFunc->result();
    PortStruct target = downloadFileResultFunc->target();

    if(target.type() == "UDP")
    {
        DownloadFileResultCmd *downloadFileResultCmd = new DownloadFileResultCmd();
        downloadFileResultCmd->setResult(result);
        QSharedPointer<BaseCmd> sharedDownloadFileResultCmd(downloadFileResultCmd);
        sendCmd(target,sharedDownloadFileResultCmd);
    }
}
